#include "tap.h"
#include "timestamp.h"
#include <string.h>

const struct test_t
{
  timestamp_t exp;
  const char *str;
} tests[] = {
  { { INT64_C(-62135596800), 0, 0 }, "0001-01-01T00:00:00Z" },
  { { INT64_C(-62135683140), 0, 1439 }, "0001-01-01T00:00:00+23:59" },
  { { INT64_C(-62135510460), 0, -1439 }, "0001-01-01T00:00:00-23:59" },
  { { INT64_C(253402300799), 0, 0 }, "9999-12-31T23:59:59Z" },
  { { INT64_C(253402214459), 0, 1439 }, "9999-12-31T23:59:59+23:59" },
  { { INT64_C(253402387139), 0, -1439 }, "9999-12-31T23:59:59-23:59" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00Z" },
  { { 1, 0, 0 }, "1970-01-01T00:00:01Z" },
  { { 10, 0, 0 }, "1970-01-01T00:00:10Z" },
  { { 60, 0, 0 }, "1970-01-01T00:01:00Z" },
  { { 600, 0, 0 }, "1970-01-01T00:10:00Z" },
  { { 3600, 0, 0 }, "1970-01-01T01:00:00Z" },
  { { 36000, 0, 0 }, "1970-01-01T10:00:00Z" },
  { { 68169600, 0, 0 }, "1972-02-29T00:00:00Z" },
  { { 0, 123456789, 0 }, "1970-01-01T00:00:00.123456789Z" },
  { { 0, 123456780, 0 }, "1970-01-01T00:00:00.12345678Z" },
  { { 0, 123456700, 0 }, "1970-01-01T00:00:00.1234567Z" },
  { { 0, 123456000, 0 }, "1970-01-01T00:00:00.123456Z" },
  { { 0, 123450000, 0 }, "1970-01-01T00:00:00.12345Z" },
  { { 0, 123400000, 0 }, "1970-01-01T00:00:00.1234Z" },
  { { 0, 123000000, 0 }, "1970-01-01T00:00:00.123Z" },
  { { 0, 120000000, 0 }, "1970-01-01T00:00:00.12Z" },
  { { 0, 100000000, 0 }, "1970-01-01T00:00:00.1Z" },
  { { 0, 10000000, 0 }, "1970-01-01T00:00:00.01Z" },
  { { 0, 1000000, 0 }, "1970-01-01T00:00:00.001Z" },
  { { 0, 100000, 0 }, "1970-01-01T00:00:00.0001Z" },
  { { 0, 10000, 0 }, "1970-01-01T00:00:00.00001Z" },
  { { 0, 1000, 0 }, "1970-01-01T00:00:00.000001Z" },
  { { 0, 100, 0 }, "1970-01-01T00:00:00.0000001Z" },
  { { 0, 10, 0 }, "1970-01-01T00:00:00.00000001Z" },
  { { 0, 1, 0 }, "1970-01-01T00:00:00.000000001Z" },
  { { 0, 9, 0 }, "1970-01-01T00:00:00.000000009Z" },
  { { 0, 90, 0 }, "1970-01-01T00:00:00.00000009Z" },
  { { 0, 900, 0 }, "1970-01-01T00:00:00.0000009Z" },
  { { 0, 9000, 0 }, "1970-01-01T00:00:00.000009Z" },
  { { 0, 90000, 0 }, "1970-01-01T00:00:00.00009Z" },
  { { 0, 900000, 0 }, "1970-01-01T00:00:00.0009Z" },
  { { 0, 9000000, 0 }, "1970-01-01T00:00:00.009Z" },
  { { 0, 90000000, 0 }, "1970-01-01T00:00:00.09Z" },
  { { 0, 900000000, 0 }, "1970-01-01T00:00:00.9Z" },
  { { 0, 990000000, 0 }, "1970-01-01T00:00:00.99Z" },
  { { 0, 999000000, 0 }, "1970-01-01T00:00:00.999Z" },
  { { 0, 999900000, 0 }, "1970-01-01T00:00:00.9999Z" },
  { { 0, 999990000, 0 }, "1970-01-01T00:00:00.99999Z" },
  { { 0, 999999000, 0 }, "1970-01-01T00:00:00.999999Z" },
  { { 0, 999999900, 0 }, "1970-01-01T00:00:00.9999999Z" },
  { { 0, 999999990, 0 }, "1970-01-01T00:00:00.99999999Z" },
  { { 0, 999999999, 0 }, "1970-01-01T00:00:00.999999999Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.0Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.00Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.000Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.0000Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.00000Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.000000Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.0000000Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.00000000Z" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00.000000000Z" },
  { { 0, 0, 1439 }, "1970-01-01T23:59:00+23:59" },
  { { 0, 0, 120 }, "1970-01-01T02:00:00+02:00" },
  { { 0, 0, 90 }, "1970-01-01T01:30:00+01:30" },
  { { 0, 0, 60 }, "1970-01-01T01:00:00+01:00" },
  { { 0, 0, 1 }, "1970-01-01T00:01:00+00:01" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00+00:00" },
  { { 0, 0, -1 }, "1969-12-31T23:59:00-00:01" },
  { { 0, 0, -60 }, "1969-12-31T23:00:00-01:00" },
  { { 0, 0, -90 }, "1969-12-31T22:30:00-01:30" },
  { { 0, 0, -120 }, "1969-12-31T22:00:00-02:00" },
  { { 0, 0, -1439 }, "1969-12-31T00:01:00-23:59" },
  { { 0, 0, 0 }, "1970-01-01T00:00:00z" },
  { { 0, 0, 0 }, "1970-01-01 00:00:00Z" },
  { { 0, 0, 0 }, "1970-01-01t00:00:00Z" },
  { { 0, 0, 0 }, "1970-01-01 00:00:00+00:00" },
};

int
main()
{
  int i, ntests;

  ntests = sizeof(tests) / sizeof(*tests);
  for (i = 0; i < ntests; i++) {
    const struct test_t t = tests[i];
    timestamp_t ts;
    int ret;

    ret = timestamp_parse(t.str, strlen(t.str), &ts);
    cmp_ok(ret, "==", 0, "timestamp_parse(\"%s\")", t.str);
    ret = timestamp_compare(&ts, &t.exp);
    cmp_ok(ret, "==", 0, "timestamp_compare(\"%s\")", t.str);
    cmp_ok(ts.offset, "==", t.exp.offset, "offset (%s)", t.str);
  }
  done_testing();
}
